package com.wangwenjun.concurrency.chapter14.exchanger;

import org.junit.Test;

import java.sql.SQLOutput;
import java.util.concurrent.Exchanger;
import java.util.concurrent.TimeUnit;

/**
 *
 * 1. Exchanger只适用于2个线程
 * 2. Exchanger交换得到的值是对象的引用而不是拷贝
 * 3. Exchanger可以被反复使用不像CountDownLatch只能使用1次
 */
public class TestExchanger {

    public static void main(String[] args) {
        test03();
    }



    public static void test01(){

        Exchanger<String> exchanger = new Exchanger<>();

        new Thread(()->{
            try {
                String result = exchanger.exchange("t1");
                System.out.println(Thread.currentThread().getName()+" get result:"+result);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }, "t1").start();


        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
                String result = exchanger.exchange("t2");
                System.out.println(Thread.currentThread().getName()+" get result:"+result);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"t2").start();
    }

    /**
     * Exchanger交换得到的值是对象的引用而不是拷贝
     */
    public static void test02(){

        Exchanger<Student> exchanger = new Exchanger<>();

        new Thread(()->{
            try {
                Student result = exchanger.exchange(new Student());
                System.out.println(Thread.currentThread().getName()+" get data:"+result);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }, "t1").start();


        new Thread(()->{
            try {
                Student student = new Student();
                System.out.println(Thread.currentThread().getName()+" send data:"+student);
                TimeUnit.SECONDS.sleep(2);
                exchanger.exchange(student);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"t2").start();
    }

    /**
     *
     * Exchanger可以被反复使用不像CountDownLatch只能使用1次
     *
     */
    public static void test03(){

        Exchanger<Integer> exchanger = new Exchanger<>();

        new Thread(()->{
            int num = 0;
            while(true){
                try {
                    Integer exchange = exchanger.exchange(num);
                    num = ++exchange;
                    System.out.println(Thread.currentThread().getName()+" get result:"+num);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "t1").start();


        new Thread(()->{
            int num = 0;
            while(true){
                try {
                    Integer exchange = exchanger.exchange(num);
                    num = ++exchange;
                    System.out.println(Thread.currentThread().getName()+" get result:"+num);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"t2").start();
    }



    private static class Student{

    }

}
